﻿/*************************************************************************
 * 
 * Filename :product
 * 
 * steven hu   2010/3/12 9:41:37
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Web;
using Hxj.Data;
using Hxj.Data.Common;
using Hxj.Common;
using RicciWebsite.Model;

namespace RicciWebsite.admin.products
{
    public partial class product : RicciWebsite.Pages.AdminPage
    {

        /// <summary>
        /// 产品ID
        /// </summary>
        protected Guid? pid;

        protected void Page_Load(object sender, EventArgs e)
        {
            string tempid = Request.QueryString["pid"];

            if (Validator.IsGuid(tempid))
            {
                pid = new Guid(tempid);
            }

            if (!IsPostBack)
            {
                Common.BindCategory(txtpcid);

                if (pid.HasValue)
                {
                    //修改
                    imgphoto.Visible = true;

                    bindProduct();

                    BindProductProperty();


                }
                else
                {
                    //添加
                    txtsubtime.Text = DateTime.Now.ToString("yyyy-MM-dd");

                    imgphoto.Visible = false;
                }

                SetTab(0);
            }
        }


        void bindProduct()
        {
            data_products product = RicciDatabase.Default.From<data_products>().Where(data_products._.pid == pid.Value.ToString()).ToFirst();
            if (null == product)
            {
                JavaScript.MsgBox_Redirect_End("该产品不存在!", "productlist.aspx");
            }
            else
            {
                txtdescription.Text = product.description;
                txtdetail.Value = product.detail;
                txtkeywords.Text = product.keywords;
                txtpcid.SelectedValue = product.pcid;
                txtpmodel.Text = product.pmodel;
                txtpname.Text = product.pname;
                txtstatus.Value = product.status.ToString();
                txtsubtime.Text = product.subtime.ToShortDateString();

                if (!string.IsNullOrEmpty(product.imgpathbig))
                    imgphoto.ImageUrl = product.imgpathbig;
                else
                    imgphoto.Visible = false;
            }
        }

        /// <summary>
        /// 绑定产品图片
        /// </summary>
        void bindfiles()
        {
            if (pid.HasValue)
            {
                rptfiles.DataSource = RicciDatabase.Default.From<base_attachment>().Where(base_attachment._.attfkid == pid.Value.ToString()).ToList();
                rptfiles.DataBind();
            }
        }

        /// <summary>
        /// 确认删除
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        protected string GetConfirm(object filename)
        {
            return "return confirm(\"确认删除" + Convert.ToString(filename) + "?\");";
        }

        /// <summary>
        /// 提交产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;
            data_products productModel = new data_products();

            if (pid.HasValue)
            {
                //修改
                productModel.pid = pid.Value.ToString();
                productModel.Attach();

            }
            else
            {
                //新增
                productModel.pid = Guid.NewGuid().ToString();
            }

            productModel.detail = txtdetail.Value;
            EntityUtils.UpdateModel<data_products>(productModel, "txt");

            using (DbTrans trans = RicciDatabase.Default.BeginTransaction())
            {
                if (pid.HasValue)
                {
                    trans.Update<data_products>(productModel);
                }
                else
                {
                    trans.Insert<data_products>(productModel);
                    List<data_products_property> pps = ProductsProperty;
                    foreach (data_products_property pp in pps)
                    {
                        pp.pid = productModel.pid;
                        trans.Insert<data_products_property>(pp);
                    }
                }
                Hxj.Web.UploadFile upfile = new UploadFile();
                //upfile.AllowFileExtensions = "jpg";


                trans.Commit();
            }

        }


        /// <summary>
        /// 提交属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddAtt_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;

            data_products_property pp = new data_products_property();
            bool isUpdate = false;

            if (!string.IsNullOrEmpty(hidppid.Value))
            {
                isUpdate = true;
            }


            EntityUtils.UpdateModel<data_products_property>(pp, "pp");
            pp.detail = ppdetail.Value;

            if (isUpdate)
            {
                pp.ppid = hidppid.Value;
                Update(pp);
                JavaScript.MsgBox("修改成功!");
            }
            else
            {
                pp.ppid = Guid.NewGuid().ToString();
                Insert(pp);
                JavaScript.MsgBox("添加成功!");

                ppppname.Text = string.Empty;
                ppdetail.Value = string.Empty;
            }

            BindProductProperty();

            SetTab(2);
        }

        /// <summary>
        /// 切换tab
        /// </summary>
        /// <param name="index"></param>
        protected void SetTab(int index)
        {
            JavaScript.RegisterStartupScript("$(document).ready(function(){beginTab(" + index.ToString() + ");})", true);
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            hidppid.Value = string.Empty;
            ppppname.Text = string.Empty;
            ppdetail.Value = string.Empty;
            btnCancel.Visible = false;
            btnAddAtt.Text = "添加";

            SetTab(2);
        }



        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptAttList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                //删除
                Delete(e.CommandArgument.ToString());

                btnCancel_Click(null, null);
                ///
                BindProductProperty();

            }
            else if (e.CommandName == "Upd")
            {
                data_products_property pp = Get(e.CommandArgument.ToString());
                if (null != pp)
                {
                    //修改
                    btnCancel.Visible = true;

                    ppppname.Text = pp.ppname;
                    ppdetail.Value = pp.detail;

                    hidppid.Value = pp.ppid;

                    btnAddAtt.Text = "修改";

                }
                else
                {
                    JavaScript.MsgBox("该属性已不存在!");
                }
                SetTab(2);
            }


        }


        /// <summary>
        /// 绑定产品属性
        /// </summary>
        protected void BindProductProperty()
        {
            if (pid.HasValue)
            {
                rptAttList.DataSource = RicciDatabase.Default.From<data_products_property>().Where(data_products_property._.pid == pid.Value.ToString()).ToList();
            }
            else
            {
                rptAttList.DataSource = ProductsProperty;
            }

            rptAttList.DataBind();
        }


        /// <summary>
        /// 获取产品属性
        /// </summary>
        /// <param name="productproperty"></param>
        protected data_products_property Get(string ppid)
        {
            if (pid.HasValue)
            {
                return RicciDatabase.Default.From<data_products_property>().Where(data_products_property._.ppid == ppid).ToFirst();
            }
            else
            {
                List<data_products_property> pps = ProductsProperty;

                return pps.Find(delegate(data_products_property ppin) { return string.Compare(ppin.ppid, ppid, true) == 0; });

            }
        }


        /// <summary>
        /// 添加产品属性
        /// </summary>
        /// <param name="productproperty"></param>
        protected void Insert(data_products_property productproperty)
        {
            if (pid.HasValue)
            {
                productproperty.pid = pid.Value.ToString();

                RicciDatabase.Default.Insert<data_products_property>(productproperty);
            }
            else
            {
                List<data_products_property> pps = ProductsProperty;

                pps.Add(productproperty);

                ProductsProperty = pps;
            }
        }


        /// <summary>
        /// 删除产品属性
        /// </summary>
        /// <param name="productproperty"></param>
        protected void Delete(string ppid)
        {
            if (pid.HasValue)
            {
                RicciDatabase.Default.Delete<data_products_property>(ppid);
            }
            else
            {
                List<data_products_property> pps = ProductsProperty;

                pps.RemoveAll(delegate(data_products_property ppin) { return string.Compare(ppin.ppid, ppid, true) == 0; });

                ProductsProperty = pps;

            }
        }


        /// <summary>
        /// 修改产品属性
        /// </summary>
        /// <param name="productproperty"></param>
        protected void Update(data_products_property productproperty)
        {
            if (pid.HasValue)
            {
                Dictionary<Field, object> fields = new Dictionary<Field, object>();
                fields.Add(data_products_property._.ppname, productproperty.ppname);
                fields.Add(data_products_property._.detail, productproperty.detail);

                RicciDatabase.Default.Update<data_products_property>(fields, data_products_property._.ppid == productproperty.ppid);
            }
            else
            {
                List<data_products_property> pps = ProductsProperty;

                data_products_property pp = pps.Find(delegate(data_products_property ppin) { return string.Compare(ppin.ppid, productproperty.ppid, true) == 0; });
                if (null != pp)
                {
                    pp.ppname = productproperty.ppname;
                    pp.detail = productproperty.detail;
                }
                ProductsProperty = pps;
            }
        }



        private string viewstateKey = "_ppkey_";

        /// <summary>
        /// 设置、读取产品属性
        /// </summary>
        protected List<data_products_property> ProductsProperty
        {
            get
            {
                if (null == ViewState[viewstateKey])
                {
                    return new List<RicciWebsite.Model.data_products_property>();
                }

                return (List<data_products_property>)ViewState[viewstateKey];
            }
            set
            {
                ViewState[viewstateKey] = value;
            }
        }


        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptfiles_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                base_attachment attchment = RicciDatabase.Default.From<base_attachment>().Where(base_attachment._.attid == e.CommandArgument.ToString()).ToFirst();
                if (null != attchment)
                {
                    FileHelper.DeleteFile(Server.MapPath(attchment.filepath));

                    RicciDatabase.Default.Delete<base_attachment>(attchment.attfkid);
                }

                bindfiles();

                JavaScript.MsgBox("删除成功!");

                SetTab(1);
            }
        }



    }
}
